/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package drexel.cs544.nap.log;

import java.io.InputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author rch_ltp
 */
public class LoggerSetup {

    public static void init(File fLog) throws Exception
    {
        if (!fLog.getParentFile().exists())
            fLog.mkdir();

        InputStream inProp = LoggerSetup.class.getResourceAsStream("log4j.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(inProp));
        File fTemp = File.createTempFile("log4j", ".properties");
        BufferedWriter bw = new BufferedWriter(new FileWriter(fTemp));
        String line;
        while((line = br.readLine()) != null)
        {
           if (line.toLowerCase().contains("log4j.appender.nap.file="))
               line = "log4j.appender.NAP.File=" + fLog.getAbsolutePath();
           bw.write(line);
           bw.newLine();
        }
        br.close();
        bw.close();
        
        PropertyConfigurator.configure(fTemp.getAbsolutePath());
    }
}
